﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ToursAgencyDAL;

namespace TourAgency
{
    public partial class Search : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        [System.Web.Services.WebMethodAttribute(), System.Web.Script.Services.ScriptMethodAttribute()]
        public static string[] GetCompletionList(string prefixText, int count, string contextKey)
        {
            // Create array of movies  
            string[] destinations = ToursAgencyDataFuncs.GetAllDestinationsToArray();

            // Return matching movies  
            return (from m in destinations where m.StartsWith(prefixText, StringComparison.CurrentCultureIgnoreCase) select m).Take(count).ToArray();
        }

        protected void ClearStartDate(object sender, EventArgs e)
        {
            this.textBoxStartDate.Text = "";
        }

        protected void ClearEndDate(object sender, EventArgs e)
        {
            this.textBoxEndDate.Text = "";
        }

        protected void buttonSearch_Click(object sender, EventArgs e)
        {
            string destination = null;
            if (this.textBoxDestination.Text != "")
            {
                destination = this.textBoxDestination.Text;
            }

            CultureInfo currentCulture = System.Threading.Thread.CurrentThread.CurrentCulture;
            CultureInfo culture = new CultureInfo("en-US");
            System.Threading.Thread.CurrentThread.CurrentCulture = culture;

            DateTime? startDate = null;
            if (this.textBoxStartDate.Text != "")
            {
                startDate = DateTime.ParseExact(this.textBoxStartDate.Text, "MM-dd-yyyy", culture);
            }

            DateTime? endDate = null;
            if (this.textBoxEndDate.Text != "")
            {
                startDate = DateTime.ParseExact(this.textBoxEndDate.Text, "MM-dd-yyyy", culture);
            }

            decimal? minPrice = null;
            if (this.textBoxMinPrice.Text != "")
            {
                minPrice = decimal.Parse(this.textBoxMinPrice.Text);
            }

            decimal? maxPrice = null;
            if (this.textBoxMaxPrice.Text != "")
            {
                maxPrice = decimal.Parse(this.textBoxMaxPrice.Text);
            }

            int? refNumber = null;
            if (this.textBoxRefNumber.Text != "")
            {
                refNumber = int.Parse(this.textBoxRefNumber.Text);
            }

            var searchResult = ToursAgencyDataFuncs.FindTour(destination, minPrice, maxPrice, startDate, endDate, refNumber);
            Session["searchResult"] = searchResult;
            System.Threading.Thread.CurrentThread.CurrentCulture = currentCulture;
            Response.Redirect("~/Offers.aspx");
        }
    }
}